package com.dongge.service;

import com.dongge.dao.CURDDaoBase;
import com.dongge.entity.Right;
import com.dongge.entity.Role;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created by Administrator on 2015/11/3.
 */
@Service
public class RoleService extends CURDDaoBase<Role> {

    @Resource
    private RightService rightService;

    /**
     * 获取当前角色所有权限的id数组
     * @param id
     * @return
     */
    public long[] getRoleRightIdsById(Long id) {
        if(id == null)
            return null;
        Role role = get(id);
        Set<Right> roleRights = role.getRights();
        int i = 0;
        long[] rightIds = new long[roleRights.size()];
        for (Right right : roleRights) {
            rightIds[i] = right.getId();
            i = i + 1;
        }
        return rightIds;
    }

    /**
     * 更新角色的权限
     * @param id
     * @param rightIds
     */
    public void updateRoleRightById(Long id,long[] rightIds){
        if(id == null || rightIds == null)
            return;
        Role role = get(id);
        for(long rightId:rightIds){
            Right right = rightService.get(rightId);
            role.getRights().add(right);
        }
    }
}
